﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using SMS.Controls.AuthenticationSVC;
using SMS.Common.Silverlight;
using SMS.Entities.Admin;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace SMS.Model
{
    public class AuthenticationModel
    {
        ServiceProxy<AuthenticationServiceClient> proxy = null;
        public event Action<ApplicationUser> AuthenticationCompleted;
        public event Action<int> OnResetPasswordCompleted;
        public event Action<ApplicationUser> SaveNewUserCompleted;
        public event Action<ObservableCollection<ApplicationUser>> GetApplicationUserListCompleted;
        public event Action<int> AdminResetPasswordCompleted;
        public event Action<SMS.Entities.Master.Branch> GetBranchDetailCompleted;
        
        public AuthenticationModel()
        {
            proxy = new ServiceProxy<AuthenticationServiceClient>("Service/AuthenticationService.svc");
            proxy.Client.AuthenticateUserCompleted += new EventHandler<AuthenticateUserCompletedEventArgs>(Client_AuthenticateUserCompleted);
            proxy.Client.ResetPasswordCompleted += new EventHandler<ResetPasswordCompletedEventArgs>(Client_ResetPasswordCompleted);
            proxy.Client.SaveNewUserCompleted += new EventHandler<SaveNewUserCompletedEventArgs>(Client_SaveNewUserCompleted);
            proxy.Client.GetApplicationUserListCompleted += new EventHandler<GetApplicationUserListCompletedEventArgs>(Client_GetApplicationUserListCompleted);
            proxy.Client.AdminResetPasswordCompleted += new EventHandler<AdminResetPasswordCompletedEventArgs>(Client_AdminResetPasswordCompleted);
            proxy.Client.GetBranchDetailCompleted += new EventHandler<GetBranchDetailCompletedEventArgs>(Client_GetBranchDetailCompleted);
        }

        void Client_GetBranchDetailCompleted(object sender, GetBranchDetailCompletedEventArgs e)
        {
            if (GetBranchDetailCompleted != null)
                GetBranchDetailCompleted(e.Result);
        }

        void Client_AdminResetPasswordCompleted(object sender, AdminResetPasswordCompletedEventArgs e)
        {
            if (AdminResetPasswordCompleted != null)
                AdminResetPasswordCompleted(e.Result);
        }

        void Client_GetApplicationUserListCompleted(object sender, GetApplicationUserListCompletedEventArgs e)
        {
            if (GetApplicationUserListCompleted != null)
                GetApplicationUserListCompleted(e.Result);
        }

        void Client_SaveNewUserCompleted(object sender, SaveNewUserCompletedEventArgs e)
        {
            if (SaveNewUserCompleted != null)
                SaveNewUserCompleted(e.Result);
        }

        void Client_ResetPasswordCompleted(object sender, ResetPasswordCompletedEventArgs e)
        {
            if (OnResetPasswordCompleted != null)
                OnResetPasswordCompleted(e.Result);
        }

        void Client_AuthenticateUserCompleted(object sender, AuthenticateUserCompletedEventArgs e)
        {
            if (AuthenticationCompleted != null)
                AuthenticationCompleted(e.Result);
        }

        public void AuthenticateUser(string userName, string Password, bool IsLoggedIn)
        {            
            proxy.Client.AuthenticateUserAsync(userName, Password, IsLoggedIn);
        }

        public void ResetPassword(string username, string currentPassword, string newPassword)
        {
            proxy.Client.ResetPasswordAsync(username, currentPassword, newPassword);
        }

        public void SaveNewUser(string username, ApplicationUser applicationUser)
        {
            proxy.Client.SaveNewUserAsync(username, applicationUser);
        }

        public void GetApplicationUserList(string username, string searchText)
        {
            proxy.Client.GetApplicationUserListAsync(username, searchText);
        }

        public void AdminResetPassword(string username, string password)
        {
            proxy.Client.AdminResetPasswordAsync(username,password);
        }

        public void GetBranchDetail(int Branch_ID)
        {
            proxy.Client.GetBranchDetailAsync(Branch_ID);
        }
    }
}
